const {prarmsErr,emptyErr,prarmsNameErr} = require('../utils/errMsg.type')

const verifyUserAndProjID = async (ctx, next)=>{
  try {
    ctx.verifyParams({
      projId: { type: 'id', required: true },
      userId: { type: 'string', required: true },
    })
  } catch (err) {
    prarmsErr.err = err
    return ctx.app.emit('errMsg', prarmsErr, ctx)
  }
  await next()
}

const verifyPoint = async (ctx, next)=>{
  try {
    ctx.verifyParams({
      num: { type: 'number', required: true },
      projId: { type: 'number', required: true },
    })
  } catch (err) {
    prarmsErr.err = err
    return ctx.app.emit('errMsg', prarmsErr, ctx)
  }
  await next()
}

const verifyPiD = async (ctx, next)=>{
  let paramsObj = {}
  if(ctx.request.method == 'POST'){
     paramsObj = ctx.request.body
  }else if(ctx.request.method == 'GET'){
    paramsObj = ctx.query
  }
  if(Object.keys(paramsObj).length == 0){
    // 参数不存在的情况
    return ctx.app.emit('errMsg', emptyErr, ctx)
  }else{
    //参数存在但错误的情况
    for(let k in paramsObj){
      if(k != 'projId') return ctx.app.emit('errMsg', prarmsNameErr, ctx)
    }
  }
  await next()
}

const verifyUserID = async (ctx, next)=>{
  let paramsObj = ctx.query
  if(Object.keys(paramsObj).length == 0){
    // 参数不存在的情况
    return ctx.app.emit('errMsg', emptyErr, ctx)
  }else{
    //参数存在但错误的情况
    for(let k in paramsObj){
      if(k != 'userId') return ctx.app.emit('errMsg', prarmsNameErr, ctx)
    }
  }
  await next()
}

// 基本信息校验
const verifyParams = async (ctx, next) => {
  try {
    ctx.verifyParams({
      grade: { type: 'string', required: true },
      userId: { type: 'id', required: true },
      gender: { type: 'string', required: true,  },
      schoolId: { type: 'id', required: true,  },
      nickName: { type: 'string', required: true,  },
      department: { type: 'string', required: true,  },
    })
  } catch (err) {
    prarmsErr.err = err
    return ctx.app.emit('errMsg', prarmsErr, ctx)
  }

  await next()
}
// 编辑简历参数校验
const verifyResumParams = async (ctx, next) => {
  try {
    ctx.verifyParams({
      userId: { type: 'string', required: true },
      schoolId: { type: 'number', required: true },
      // department: { type: 'number', required: true },
      grade: { type: 'id', required: true },
      nickName: { type: 'string', required: true },
      gender: { type: 'number', required: true },
      selfdesc: { type: 'string', allowEmpty:true },
      skills: { type: 'string', allowEmpty:true },
      exp: { type: 'string', allowEmpty:true},
    })
  } catch (err) {
    prarmsErr.err = err
    return ctx.app.emit('errMsg', prarmsErr, ctx)
  }

  await next()
}
// 查询项目参数校验
const verifyProjParams = async (ctx, next) => {
  try {
    ctx.verifyParams({
      schoolId: { type: 'id', required: true },
      limit: { type: 'number', required: true },
      page: { type: 'number', required: true },
      department: { type: 'string', allowEmpty:true},
      grade: { type: 'string' ,allowEmpty:true},
      projName: { type: 'string',allowEmpty:true },
      sortId: { type: 'id',allowEmpty:true},
      num: { type: 'array' ,max: 2},
    })
  } catch (err) {
    // console.log(err);
    prarmsErr.err = err
    return ctx.app.emit('errMsg', prarmsErr, ctx)
  }

  await next()
}
// 增加项目参数校验
const verifyaddProjParams = async (ctx, next) => {
  try {
    ctx.verifyParams({
      department:{type: 'string', required: true, allowEmpty: true },
      grade: { type: 'string', required: true, allowEmpty: true },
      num: { type: 'number', required: true },
      sortId: { type: 'string', required: true },
      userId: { type: 'string', required: true },
      schoolId: { type: 'id', required: true },
      projDesc: { type: 'string', required: true },
      projName: { type: 'string', required: true },
    })
  } catch (err) {
    prarmsErr.err = err
    return ctx.app.emit('errMsg', prarmsErr, ctx)
  }

  await next()
}
//
const verifydeliver  = async (ctx, next) => {
  try {
    ctx.verifyParams({
      senderId: { type: 'string', required: true },
      projId: { type: 'id', required: true },
      reciverId: { type: 'string', required: true },
      contact :{type: 'string', required: true}
    })
  } catch (err) {
    prarmsErr.err = err
    return ctx.app.emit('errMsg', prarmsErr, ctx)
  }

  await next()
}

const verifydeliverStatus = async (ctx, next) => {
  try {
    ctx.verifyParams({
      senderId: { type: 'string', required: true },
      projId: { type: 'id', required: true },
      reciverId: { type: 'string', required: true },
    })
  } catch (err) {
    prarmsErr.err = err
    return ctx.app.emit('errMsg', prarmsErr, ctx)
  }
  await next()
}

const verifysendFeedback = async (ctx, next) => {
  try {
    ctx.verifyParams({
      userId: { type: 'string', required: true },
      info: { type: 'string', required: true },
    })
  } catch (err) {
    prarmsErr.err = err
    return ctx.app.emit('errMsg', prarmsErr, ctx)
  }
  await next()
}

module.exports = {
  verifyParams,
  verifyResumParams,
  verifyProjParams,
  verifyaddProjParams,
  verifyUserID,
  verifyPiD,
  verifyPoint,
  verifyUserAndProjID,
  verifydeliver,
  verifydeliverStatus,
  verifysendFeedback
}
  